package com.example.controller.siyunDingdanController;

import com.example.pojo.siyunDingdanPojo.*;
import com.example.service.siyunDingdanService.SiyunDingdanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
public class SiyunDingdanController {
    @Autowired
    private SiyunDingdanService siyunDingdanService;
    @Value("${file.path}")
    private String path;

    /*
    * 此方法用来获取订单的各个状态的数量
    * 李宗儒
    * 2022.11.11
    * */
    //
    @PostMapping("/postGetDingdanAll")
    public SiyunDingdanStatus getDingdanAll(@RequestBody SiyunDingdan siyunDingdan){
        return siyunDingdanService.getShopDingdanAlltype(siyunDingdan.getOrderType());
    }

    /*
    * 此方法用来获取全部数据
    * 李宗儒
    * 2022.11.11
    * */
    @PostMapping("/postGetDingdanTableAll")
    public List<SiyunDingdan> getDingdanAllTable(@RequestBody SiyunDingdan siyunDingdan){
        return siyunDingdanService.getShopDingdanAll(siyunDingdan);
    }

    /*
    根据用户筛选的条件进行查询
    * 李宗儒
    * 2022.11.11
    * */
    @PostMapping("/postGetDingdanMohucha")
    public List<SiyunDingdan> getMohuCha(@RequestBody SiyunDingdan siyunDingdan){
        System.out.println(siyunDingdan);
        List<SiyunDingdan> mohuCha = siyunDingdanService.getShopMohuCha(siyunDingdan);
        return mohuCha;
    }
    /**
    * @Author: lzr
    * @Description:当点击查询订单时根据订单里面的信息来确定展现给用户的数据
    * @DateTime: 15:58 2022/11/13
    * @Params:
    * @Return
    */
    @PostMapping("/postGetShopDingdanChaxunAll")
    public SiyunShopDingdanPlus getShopDingdanChaxunAll(@RequestBody SiyunShopDingdanPlus siyunShopDingdanPlus){
        System.out.println(siyunShopDingdanPlus);
        return siyunDingdanService.getShopDingdanChaxunAll(siyunShopDingdanPlus);
    }
    /**
    * @Author: lzr
    * @Description:当点击删除的时候把选择的数据删除
    * @DateTime: 17:46 2022/11/20
    * @Params:
    * @Return
    */
    @PostMapping("/GetShopDelete")
    public boolean getShopDelete(@RequestBody SiyunDingdanDelete siyunDingdanDelete){
        boolean key = false;
        Map<String ,Boolean > map = new HashMap<>();
        String orderNo = siyunDingdanDelete.getOrderNo();
        String[] split = orderNo.split(",");
        for (String s : split) {
            Integer it=Integer.valueOf(s);
            Boolean shopDelete = siyunDingdanService.getShopDelete(it);
            key = shopDelete;
            map.put("status",shopDelete);
        }
        return key;
    }
    
    /**
    * @Author: lzr
    * @Description: 修改收货人的基本信息
    * @DateTime: 15:37 2022/11/22
    * @Params: 
    * @Return 
    */
    @PostMapping("/UpdateShopOrder")
    public boolean getUpdateOrder(@RequestBody SiyunShopDingdanPlus siyunShopDingdanPlus){
        return siyunDingdanService.getUpdateDingdan(siyunShopDingdanPlus);
    }
    /**
    * @Author: lzr
    * @Description: 备注订单
    * @DateTime: 15:38 2022/11/22
    * @Params: 
    * @Return 
    */
    @PostMapping("/UpdateRemarks")
    public boolean getUpdateRemarks(@RequestBody SiyunShopDingdanPlus siyunShopDingdanPlus){
        return siyunDingdanService.getUpdateRemarks(siyunShopDingdanPlus);
    }
    /**
    * @Author: lzr
    * @Description: 搜索商品订单
    * @DateTime: 10:47 2022/11/23
    * @Params:
    * @Return
    */
    @PostMapping("/selectShop")
    public SiyunShopDingdanPlus getSousuoShop(@RequestBody SiyunShopDingdanPlus siyunShopDingdanPlus){
        return siyunDingdanService.getSousuoShop(siyunShopDingdanPlus);
    }

    //添加文件路径
    @RequestMapping("/getUpdateImg")
    public boolean getUpdateImg(MultipartFile file,SiyunShopDingdanPlus siyunShopDingdanPlus){
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.indexOf(".");
        String substring = originalFilename.substring(i);
        UUID uuid = UUID.randomUUID();
        File file1 = new File(path+uuid+substring);
        try {
            file.transferTo(file1);
            siyunShopDingdanPlus.setMainImage("http://localhost:8091/Img/"+uuid+substring);
            return siyunDingdanService.getUpdateImg(siyunShopDingdanPlus);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
    * @Author: lzr
    * @Description: 根据商品编号修改商品
    * @DateTime: 19:48 2022/11/24
    * @Params: 
    * @Return 
    */
    @PostMapping("/UpdateShop")
    public boolean getUpdateShop(@RequestBody SiyunShopDingdanPlus siyunShopDingdanPlus){
        return siyunDingdanService.getUpdateShop(siyunShopDingdanPlus);
    }

    /**
    * @Author: lzr
    * @Description: 根据传来的orderId跟踪订单
    * @DateTime: 11:33 2022/11/29
    * @Params:
    * @Return
    */
    @PostMapping("/GenzongDingdan")
    public List<SiyunDingdanGenzong> getGenzongDingdan(@RequestBody SiyunDingdanGenzong siyunDingdanGenzong){
        return siyunDingdanService.getGenzongDingdan(siyunDingdanGenzong);
    }
}
